package it.koutifaris.travianmap.dal.sqlservices.tile;

import it.koutifaris.travianmap.dal.RevisionService;
import it.koutifaris.travianmap.dal.RevisionService.Revision;
import it.koutifaris.travianmap.tile.TileLoader;
import it.koutifaris.travianmap.tile.TileMapLoader;
import android.database.sqlite.SQLiteDatabase;

public class SqlTileMapLoader implements TileMapLoader {

	private final RevisionService revisionService;
	private final SQLiteDatabase database;
	private final String serverId;

	public SqlTileMapLoader(RevisionService revisionService, String serverId, SQLiteDatabase database) {
		this.revisionService = revisionService;
		this.serverId = serverId;
		this.database = database;
	}

	@Override
	public TileLoader getLoaderFor(Revision revision) {
		boolean revisionExist = revisionService.revisionExist(revision);

		TileLoader result = null;

		if (revisionExist) {
			result = new SqlTileLoader(serverId, database, revision);
		}

		return result;
	}

	@Override
	public void close() {
		database.close();
	}
}
